﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0712502_0712504_DesignPatternGame
{
    public class UserControlledTankLevel1 : UserControlledTank
    {
        public UserControlledTankLevel1(UserControlledTankManager tankManager)
        {
            _tankManager = tankManager;

            tankBodyFactory = new TankBodyFactory();
            tankTurretFactory = new TankTurretFactory();
                        
            //_iSpeed = 3;
            Life = 3;
            TimeDelayToReload = 10;
            ReloadTime = 0;

            this._bulletManager = new BulletManager();  // Quản lý đạn cho player tank cấp 1
        }

        public override void Initialize()
        {
            _exp = 0;
            _lowerExp = 0;
            _upperExp = 200;

            tankTurretFactory.InitPrototypes();
            tankBodyFactory.InitPrototypes();

            AddBody((TankBodyModel)tankBodyFactory.CreateObject(0));
            AddTurret((TankTurretModel)tankTurretFactory.CreateObject(0));
            AddEngine(new GermanyEngine());

            this.Size = new Rectangle(0, 0, 54, 54);
            this.SizeOfRectInside = new Point(48, 48);
            this.SizeOfRectOutside = new Point(54, 54);

            this.ISpeed = this.EngineTank.Speed;
            this.Amor = this.BodyTank.Amor + this.TurretTank.Amor;
            this.Dammage = this.BodyTank.Damage + this.TurretTank.Dammage;
            this.MaxHP = 300;
            this.CurrentHP = MaxHP;
            this.Position = new Vector2(300, 655);
            this.MoveState = MovingState.Stop;
        }

        public override void AddExperiencePoint(int exp)
        {
            _exp += exp;
            LevelChangeCheck();
        }

        private void LevelChangeCheck()
        {
            if (_exp > _upperExp)
            {
                _tankManager.TankLevel = new UserControlledTankLevel2(this);
                _tankManager.Initilize();
            }
        }

        public override void TankShot()
        {
            BulletLevel1 newBullet = new BulletLevel1();
            newBullet.IdTank = this.idTank;
            Vector2 direction = new Vector2(-(float)Math.Sin(-this.TurretRotationAngle), -(float)Math.Cos(-this.TurretRotationAngle));
           
            newBullet.Position = this.TurretPosition + new Vector2(20, 20) * direction;
            newBullet.RotationAngle = this.TurretRotationAngle;
            newBullet.IdTank = this.idTank;
            newBullet.ISpeed = 10;

            newBullet.TotalDamage = newBullet.Damages + this.Dammage;

           // newBullet.RectCollisionOutside = new Rectangle((int)newBullet.Position.X, (int)newBullet.Position.Y, 10, 10);
            this._bulletManager._listBullets.Add(newBullet);
            
            GameResourceContainer.SoundManager.DestBrick.Play();
        }
      
    }
}
